package com.sunwayworld.basemodule.samplemanage.location.service.impl;

import com.sunwayworld.basemodule.samplemanage.location.bean.LimsLocationBoardBean;
import com.sunwayworld.framework.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.samplemanage.location.dao.LimsLocationBoardHoleDao;
import com.sunwayworld.basemodule.samplemanage.location.bean.LimsLocationBoardHoleBean;
import com.sunwayworld.basemodule.samplemanage.location.service.LimsLocationBoardHoleService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.ArrayList;
import java.util.List;

@Repository
@GikamBean
public class LimsLocationBoardHoleServiceImpl implements LimsLocationBoardHoleService {

    @Autowired
    private LimsLocationBoardHoleDao limsLocationBoardHoleDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsLocationBoardHoleDao getDao() {
        return limsLocationBoardHoleDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsLocationBoardHoleBean limsLocationBoardHole = jsonWrapper.parseUnique(LimsLocationBoardHoleBean.class);
        limsLocationBoardHole.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsLocationBoardHole);
        return limsLocationBoardHole.getId();
    }

    /*
     * 根据板的参数，批量生成孔
     */
    @Override
    @AuditTrailEntry(AuditTrailType.INSERT)
    @Transactional
    public void insertHole(LimsLocationBoardBean boardBean) {
        List<LimsLocationBoardHoleBean> holeList = new ArrayList<>();
        for (int i = 1; i <= boardBean.getColumnNum(); i++) {
            for (int j = 1; j <= boardBean.getRowNum(); j++) {
                LimsLocationBoardHoleBean holeBean = new LimsLocationBoardHoleBean();
                holeBean.setId(ApplicationContextHelper.getNextIdentity());
                holeBean.setRowNum(boardBean.getRowNum());
                holeBean.setColNum(boardBean.getColumnNum());
                holeBean.setBoardId(boardBean.getId());
                holeBean.setX(i);
                holeBean.setY(j);
                if (!StringUtils.isEmpty(boardBean.getBoardCode())) {
                    char c = (char) (j + 64);
                    String s = String.format("%02d", i);
                    holeBean.setLocations( boardBean.getBoardCode() + "(" + c + s + ")");
                }
                holeList.add(holeBean);
            }
        }
        this.getDao().insert(holeList);
    }

    @Transactional
    @Override
    public void batchDeleteByBoardId(List<Long> ids) {
        getDao().batchDeleteByBoardId(ids);
    }

    @Override
    public List<LimsLocationBoardHoleBean> selectListBySampleIds(List<Long> sampleIds) {
        return getDao().selectListBySampleIds(sampleIds);
    }

}
